home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-03-06 | 2.5 KB | 73 lines | [TEXT/GEOL] |
- Item 0153058 8-Dec-89 08:23
-
- From: ALGER Alger, Jeff,VCA
-
- To: D1950 CSG, Don Phillips,PRT
-
- cc: MACAPP.TECH$ MacApp Technical
-
- Sub: Re: Failure Handling
-
- Jo-Anne,
-
- Your question is, "what is the best way to implement failure handling...." The
- code fragment you presented will work just fine and may be appropriate for some
- cases, not so good for others. Let's deal with some general principles instead
- of a specific case.
-
- The basic idea of a failure handler is to restore state. Before a
- failure-prone section of code is executed, the program is in state X. If a
- failure occurs between a CatchFailures and a Success, the failure handler is
- responsible for restoring the program to state X, then taking action
- appropriate when it is known that the succeeding block of code will not work.
- In pseudo-code,
-
- IF SomeCode will work if tried THEN
- SomeCcode
- ELSE
- do something else
-
- Typically, you cannot tell in advance whether the code will work properly, so
- the failure handler "backs up" to the state of the program at the IF, then
- resumes with the ELSE.
-
- To support this activity, CatchFailures stores the machine registers on its
- internal stack and Fail restores them. Thus, local variables, the stack
- pointer, etc., are all restored for you automatically. All you have to do is
- undo any intermediate damage done between the CatchFailures and the Fail, such
- as freeing newly allocated objects.
-
- The normal behavior of the failure trapping mechanism when a failure occurs is
- to restore the state of the top (most recent) failure handler, call it, then
- repeat with the next failure handler on the stack, etc. If the stack is
- exhausted, the program aborts. If you want to write a failure handler which
- does not keep percolating upward, do something like
-
- PROCEDURE TObjectX.DoSomething;
- VAR
- fi: FailInfo;
- PROCEDURE LocalFailureHandler (error: OSErr; message: LONGINT);
- BEGIN
- GOTO 1234;
- END;
- BEGIN
- CatchFailures (fi, LocalFailureHandler);
- { do something which can fail }
- Success (fi);
-
- 1234:
- { do something else }
- END;
-
- MacApp is friendly enough to do this by default by pushing a default failure
- handler which does a GOTO to resume the idle loop. Unless you take steps to
- exit the loop prematurely, your failure handlers will be called in the reverse
- order in which they were pushed using CatchFailures until this default handler
- is reached.
-
- I hope this helps.
-
- Jeff Alger
- KPMG Peat Marwick
-
-